package com.woniu.user.controller;

import com.woniu.common.pojo.*;
import com.woniu.common.vo.AjaxResult;
import com.woniu.user.service.CreatService;
import com.woniu.user.service.TypeService;
import com.woniu.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.transform.sax.SAXSource;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/creat")
@CrossOrigin
public class CreatController {
    @Resource
    private CreatService creatService;

    // 添加数据到购物车
    @RequestMapping("/addCreat")
    public AjaxResult addCreat(@RequestBody Creat creat) {
        System.out.println("Received data: " + creat); // 打印完整的对象
        try {
            creatService.creatUser(creat);
            return AjaxResult.success("成功添加到购物车");
        } catch (Exception e) {
            return AjaxResult.error("添加到购物车失败");
        }
    }

    // 查询是否存在，如果存在提示已经加入购物车
    @RequestMapping("/selectCreatOne")
    public AjaxResult selectCreatOne(@RequestParam("uid") Integer uid, @RequestParam("fid") Integer fid) {
        System.out.println(uid + "-===" + fid + "------------===");
        Creat creat = creatService.existsInCart(uid, fid);
        if (creat != null) {
            return AjaxResult.error("该商品已存在购物车");
        } else {
            AjaxResult ajaxResult = AjaxResult.success("可以加入购物车");
            System.out.println(ajaxResult);
            return ajaxResult;
        }
    }

    // 查询购物车
    @RequestMapping(value = "/selectCreat", method = RequestMethod.GET)
    public AjaxResult selectCreat(@RequestParam("uid") Integer uid) {
        List<Creat> creats = creatService.selectCreat(uid);
        for (Creat creat : creats) {
            System.out.println(creat + "ppppppppppppppp");
        }
        return AjaxResult.success("查询成功", creats);
    }

    //查询当前登入的用户购物车中有几条数据
    @RequestMapping("/selectCreatCount")
    public AjaxResult selectCreatCount(@RequestParam("uid") Integer uid) {
        Integer count = creatService.selectCount(uid);
        System.out.println("------------===" + count);
        return AjaxResult.success("查询成功", count);
    }

    // 删除购物车
    @RequestMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        for (Integer id : ids) {
            System.out.println(id + "------------===");
        }
        creatService.delete(ids);
        return AjaxResult.success("删除成功!");
    }
}
